import storage from '@system.storage';
import Config from './config.js';

// abilityType: 0-Ability; 1-Internal Ability
const ABILITY_TYPE_EXTERNAL = 0;
const ABILITY_TYPE_INTERNAL = 1;
// syncOption(Optional, default sync): 0-Sync; 1-Async
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;

const READ_DATA = 10001;
const WRITE_DATA = 10002;
const READ_TABLE = 10003;
const WRITE_TABLE = 10004;

var action = {};
action.bundleName = 'com.acedemo.game2048';
action.abilityName = 'com.acedemo.game2048.StorageAbility';
action.abilityType = ABILITY_TYPE_INTERNAL;
action.syncOption = ACTION_SYNC;

export default {
/**
     * 保存最高分
     * @param bestScore
     */
    putBestScore: async function(bestScore) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                var actionData = {
                    type: "TYPE_PUT_BEST_SCORE",
                    columnCount: 4,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_4,
                    value: bestScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('===putBestScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putBestScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putBestScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            case 5: // 难度5
            {
                var actionData = {
                    type: "TYPE_PUT_BEST_SCORE",
                    columnCount: 5,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_5,
                    value: bestScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('===putBestScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putBestScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putBestScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            case 6: // 难度6
            {
                var actionData = {
                    type: "TYPE_PUT_BEST_SCORE",
                    columnCount: 6,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_6,
                    value: bestScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('===putBestScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putBestScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putBestScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取最高分
     */
    async getBestScore() {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                var actionData = {
                    type: "TYPE_GET_BEST_SCORE",
                    columnCount: 4,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_4,
                };
                action.data = actionData;
                action.messageCode = READ_DATA;

                return FeatureAbility.callAbility(action);
                break;
            }
            case 5: // 难度5
            {
                var actionData = {
                    type: "TYPE_GET_BEST_SCORE",
                    columnCount: 5,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_5,
                };
                action.data = actionData;
                action.messageCode = READ_DATA;

                return FeatureAbility.callAbility(action);
                break;
            }
            case 6: // 难度6
            {
                var actionData = {
                    type: "TYPE_GET_BEST_SCORE",
                    columnCount: 6,
                    fileName: Config.SAVE_BEST_SCORE,
                    key: Config.KEY_BEST_SCORE_WITHIN_6,
                };
                action.data = actionData;
                action.messageCode = READ_DATA;

                return FeatureAbility.callAbility(action);
                break;
            }
            default:
                break;
        }
    },
/**
     * 保存当前得分
     * @param currentScore
     */
    async putCurrentScore(currentScore) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                var actionData = {
                    type: "TYPE_PUT_CURRENT_SCORE",
                    columnCount: 4,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_4",
                    value: currentScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('=== putCurrentScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putCurrentScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putCurrentScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            case 5: // 难度5
            {
                var actionData = {
                    type: "TYPE_PUT_CURRENT_SCORE",
                    columnCount: 5,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_5",
                    value: currentScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('=== putCurrentScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putCurrentScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putCurrentScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            case 6: // 难度6
            {
                var actionData = {
                    type: "TYPE_PUT_CURRENT_SCORE",
                    columnCount: 6,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_6",
                    value: currentScore,
                };
                action.messageCode = WRITE_DATA;
                action.data = actionData;
                var result = await FeatureAbility.callAbility(action);
                var ret = JSON.parse(result);
                console.info('=== putCurrentScore result is:' + JSON.stringify(ret));
                if (ret.code == 0) {
                    console.info('===putCurrentScore result is:' + JSON.stringify(ret.result));
                } else {
                    console.error('===putCurrentScore error code:' + JSON.stringify(ret.code));
                }
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取当前得分
     * @return bestScore
     */
    async getCurrentScore() {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                var actionData = {
                    type: "TYPE_GET_CURRENT_SCORE",
                    columnCount: 4,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_4",
                };
                action.data = actionData;
                action.messageCode = READ_DATA;
                return FeatureAbility.callAbility(action);
                break;
            }
            case 5: // 难度5
            {
                var actionData = {
                    type: "TYPE_GET_CURRENT_SCORE",
                    columnCount: 5,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_5",
                };
                action.data = actionData;
                action.messageCode = READ_DATA;
                return FeatureAbility.callAbility(action);
                break;
            }
            case 6: // 难度6
            {
                var actionData = {
                    type: "TYPE_GET_CURRENT_SCORE",
                    columnCount: 6,
                    fileName: Config.SAVE_CURRENT_SCORE,
                    key: "KEY_CURRENT_SCORE_6",
                };
                action.data = actionData;
                action.messageCode = READ_DATA;
                return FeatureAbility.callAbility(action);
                break;
            }
            default:
                break;
        }
    },
/**
     * 保存无限模式下最高分
     * @param bestScore
     */
    async putBestScoreWithinInfinite(bestScore) {
        var actionData = {
            type: "TYPE_PUT_BEST_SCORE_WITH_ININFINITE",
            fileName: Config.SAVE_BEST_SCORE,
            key: Config.KEY_BEST_SCORE_WITHIN_INFINITE,
            value: bestScore,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putBestScoreWithinInfinite result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putBestScoreWithinInfinite result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putBestScoreWithinInfinite error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取无限模式下最高分
     */
    async getBestScoreWithinInfinite() {
        var actionData = {
            type: "TYPE_GET_BEST_SCORE_WITH_ININFINITE",
            fileName: Config.SAVE_BEST_SCORE,
            key: Config.KEY_BEST_SCORE_WITHIN_INFINITE,
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
/**
     * 保存当前无限模式分数
     */
    async putCurrentScoreWithinInfinite(currentScore) {

        var actionData = {
            type: "TYPE_PUT_CURRENT_SCORE_WITHIN_INFINITE",
            fileName: Config.SAVE_CURRENT_SCORE_INFINITE,
            key: "KEY_CURRENT_SCORE_INFINITE",
            value: currentScore,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putCurrentScoreWithinInfinite result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putCurrentScoreWithinInfinite result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putCurrentScoreWithinInfinite error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取当前无限模式分数
     * @return currentScore
     */
    async getCurrentScoreWithinInfinite() {

        var actionData = {
            type: "TYPE_GET_CURRENT_SCORE_WITHIN_INFINITE",
            fileName: Config.SAVE_CURRENT_SCORE_INFINITE,
            key: "KEY_CURRENT_SCORE_INFINITE",
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
/**
     * 保存游戏难度
     * @param difficulty
     */
    async putGameDifficulty(difficulty) {
        var actionData = {
            type: "TYPE_PUT_GAME_DIFFICULTY",
            fileName: Config.SAVE_GAME_DIFFICULTY,
            key: Config.KEY_GAME_DIFFICULTY,
            value: difficulty,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putGameDifficulty result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putGameDifficulty result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putGameDifficulty error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取游戏难度
     */
    async getGameDifficulty() {
        var actionData = {
            type: "TYPE_GET_GAME_DIFFICULTY",
            fileName: Config.SAVE_GAME_DIFFICULTY,
            key: Config.KEY_GAME_DIFFICULTY,
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
/**
     * 保存游戏音效状态
     * @param volumeState
     */
    async putGameVolume(volumeState) {
        var actionData = {
            type: "TYPE_PUT_GAME_VOLUME_STATE",
            fileName: Config.SAVE_GAME_VOLUME_STATE,
            key: Config.KEY_GAME_VOLUME_STATE,
            value: volumeState,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putGameVolume result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putGameVolume result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putGameVolume error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取游戏音效状态
     */
    async getGameVolumeState() {
        var actionData = {
            type: "TYPE_GET_GAME_VOLUME_STATE",
            fileName: Config.SAVE_GAME_VOLUME_STATE,
            key: Config.KEY_GAME_VOLUME_STATE,
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
/**
     * 保存达成游戏目标次数
     * @param time
     */
    async putGoalTime(time) {
        var actionData = {
            type: "TYPE_PUT_GOAL_TIME",
            fileName: Config.SAVE_GET_GOAL_TIME,
            key: Config.KEY_GET_GOAL_TIME,
            value: time,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putGoalTime result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putGoalTime result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putGoalTime error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取达成游戏目标次数
     */
    async getGoalTime() {
        var actionData = {
            type: "TYPE_GET_GOAL_TIME",
            fileName: Config.SAVE_GET_GOAL_TIME,
            key: Config.KEY_GET_GOAL_TIME,
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
/**
     * 保存游戏模式
     * @param mode
     */
    async putCurrentGameMode(mode) {
        var actionData = {
            type: "TYPE_PUT_CURRENT_GAME_MODE",
            fileName: Config.SAVE_CURRENT_GAME_MODE,
            key: Config.KEY_CURRENT_GAME_MODE,
            value: mode,
        };
        action.messageCode = WRITE_DATA;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.info('===putCurrentGameMode result is:' + JSON.stringify(ret));
        if (ret.code == 0) {
            console.info('===putCurrentGameMode result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putCurrentGameMode error code:' + JSON.stringify(ret.code));
        }
    },
/**
     * 获取游戏模式
     */
    async getCurrentGameMode() {
        var actionData = {
            type: "TYPE_GET_CURRENT_GAME_MODE",
            fileName: Config.SAVE_CURRENT_GAME_MODE,
            key: Config.KEY_CURRENT_GAME_MODE,
        };
        action.data = actionData;
        action.messageCode = READ_DATA;
        return FeatureAbility.callAbility(action);
    },
    /**
     * 保存游戏数据
     */
    async putTableData(fileName, data) {
        var actionData = {
            type: "TYPE_PUT_TABLE_DATA",
            fileName: fileName,
            key: Config.KEY_CURRENT_GAME_DATA,
            value: data,
        };
        action.messageCode = WRITE_TABLE;
        action.data = actionData;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        if (ret.code == 0) {
            console.info('===putTableData result is:' + JSON.stringify(ret.result));
        } else {
            console.error('===putTableData error code:' + JSON.stringify(ret.code));
        }
    },
    /**
     * 获取游戏数据
     */
    async getTableData(fileName) {
        var actionData = {
            type: "TYPE_GET_TABLE_DATA",
            fileName: fileName,
            key: Config.KEY_CURRENT_GAME_DATA,
        };
        action.data = actionData;
        action.messageCode = READ_TABLE;
        return FeatureAbility.callAbility(action);
    }
}